package com.example.message.util;

import com.example.message.entity.Message;
import com.example.message.entity.MessageReceive;
import com.example.message.enums.MessageType;
import com.example.message.enums.PriorityType;
import com.example.message.enums.TargetType;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 通知工具类
 */
public class MessageUtils {
    
    /**
     * 创建系统通知
     */
    public static Message createSystemMessage(String title, String content, Long senderId, String senderName) {
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        message.setMessageType(MessageType.SYSTEM.getCode());
        message.setPriority(PriorityType.MEDIUM.getCode());
        message.setSendMethod(1); // 站内信
        message.setSenderId(senderId);
        message.setSenderName(senderName);
        message.setStatus(0); // 草稿
        message.setCreateTime(LocalDateTime.now());
        return message;
    }
    
    /**
     * 创建业务通知
     */
    public static Message createBusinessMessage(String title, String content, String businessType,
                                               String businessId, Long senderId, String senderName) {
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        message.setMessageType(MessageType.BUSINESS.getCode());
        message.setPriority(PriorityType.MEDIUM.getCode());
        message.setSendMethod(1); // 站内信
        message.setBusinessType(businessType);
        message.setBusinessId(businessId);
        message.setSenderId(senderId);
        message.setSenderName(senderName);
        message.setStatus(0); // 草稿
        message.setCreateTime(LocalDateTime.now());
        return message;
    }
    
    /**
     * 创建提醒通知
     */
    public static Message createReminderMessage(String title, String content, LocalDateTime expireTime,
                                               Long senderId, String senderName) {
        Message message = new Message();
        message.setTitle(title);
        message.setContent(content);
        message.setMessageType(MessageType.REMINDER.getCode());
        message.setPriority(PriorityType.HIGH.getCode());
        message.setSendMethod(1); // 站内信
        message.setExpireTime(expireTime);
        message.setSenderId(senderId);
        message.setSenderName(senderName);
        message.setStatus(0); // 草稿
        message.setCreateTime(LocalDateTime.now());
        return message;
    }
    
    /**
     * 创建接收记录
     */
    public static MessageReceive createReceiveRecord(Long noticeId, Long receiverId, String receiverName,
                                                     Long deptId, String deptName) {
        MessageReceive receive = new MessageReceive();
        receive.setMessageId(noticeId);
        receive.setReceiverId(receiverId);
        receive.setReceiverName(receiverName);
        receive.setDeptId(deptId);
        receive.setDeptName(deptName);
        receive.setReadStatus(0); // 未读
        receive.setHandleStatus(0); // 未处理
        receive.setCreateTime(LocalDateTime.now());
        return receive;
    }
    
    /**
     * 获取通知类型描述
     */
    public static String getMessageTypeDesc(Integer noticeType) {
        MessageType type = MessageType.getByCode(noticeType);
        return type != null ? type.getDesc() : "未知";
    }
    
    /**
     * 获取优先级描述
     */
    public static String getPriorityDesc(Integer priority) {
        PriorityType type = PriorityType.getByCode(priority);
        return type != null ? type.getDesc() : "未知";
    }
    
    /**
     * 获取目标类型描述
     */
    public static String getTargetTypeDesc(Integer targetType) {
        TargetType type = TargetType.getByCode(targetType);
        return type != null ? type.getDesc() : "未知";
    }
    
    /**
     * 构建模板参数
     */
    public static Map<String, Object> buildTemplateParams(String... params) {
        Map<String, Object> paramMap = new HashMap<>();
        for (int i = 0; i < params.length; i += 2) {
            if (i + 1 < params.length) {
                paramMap.put(params[i], params[i + 1]);
            }
        }
        return paramMap;
    }
}